package com.jjlin.chapter_2;
import java.util.*;
public class CD160{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    //创建单链表
    public static Node createNodeList(int[] nums){
        Node head = new Node(nums[0]);
        Node pre = head;
        Node cur = null;
        for(int i = 1; i < nums.length; i++){
            cur = new Node(nums[i]);
            pre.next = cur;
            pre = cur;
        }
        return head;
    }

    //打印单链表
    public static void printNodeList(Node head){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //按照左右半区的方式重新组合单链表
    public static void relocate(Node head){
        if(head == null || head.next == null)
            return;
        Node mid = head;
        Node right = head.next;
        while(right.next != null && right.next.next != null){
            right = right.next.next;
            mid = mid.next;
        }
        right = mid.next;
        mid.next = null;//这个很重要
//        return mergeLR(head, right);
        mergeLR(head, right);
    }

    //自己的写法
    public static Node mergeLR(Node left, Node right){
        Node head = new Node(-1);
        Node cur = head;
        while(left != null && right != null){
            cur.next = left;
            left = left.next;
            cur = cur.next;
            cur.next = right;
            right = right.next;
            cur = cur.next;
        }
        if(right != null)
            cur.next = right;
        return head.next;
    }

    //书本上的写法
    public static void mergeLR1(Node left, Node right){
        Node next = null;
        while(left.next != null){
            next = right.next;
            right.next = left.next;
            left.next = right;
            left = right.next;
            right = next;
        }
        left.next = right;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++){
            nums[i] = sc.nextInt();
        }
        Node head = createNodeList(nums);
//        head = relocate(head);
        relocate(head);
        printNodeList(head);
    }
}

